package com.zeei.ems.base.controller;

import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import com.zeei.ems.base.result.ResultDataVO;
import com.zeei.ems.base.result.ResultRowsVO;
import com.zeei.ems.base.service.AreaCodeService;
import com.zeei.ems.base.tree.TreeNodeVO;
import com.zeei.ems.base.util.HttpServiceUtil;
import com.zeei.ems.base.util.StringUtil;
import com.zeei.ems.base.vo.AreaCodeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @类型名称：AreaManageController
 * @类型描述：区域管理控制器
 * @功能描述：区域管理
 * @创建作者：guoyuqing
 */
@Api(value = "区域信息controller", tags = { "区域信息接口" })
@Controller
@RequestMapping("/ems/area")
public class AreaManageController extends BasicController {

	@Autowired
	private AreaCodeService areaCodeService;

	/**
	 * 
	 * queryAreaCode:(查詢區域信息，這裡傳入區域ID與區域名稱，當某一項為空時，直接獨立查找，當數據同時存在時，合併查找).
	 *
	 * @param regionCode
	 * @param regionName
	 * @param filterFlag
	 *            :有值-对用户的站点权限过滤；不传-进行过滤（2019-7）
	 * @return
	 */
	@ApiOperation(value = "查询区域信息下拉框", notes = "查询区域信息下拉框", httpMethod = "POST")
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@ResponseBody
	public ResultRowsVO<TreeNodeVO<AreaCodeVO>> queryAreaCodeTree(String regionCode, String regionName, String showALL,
																  String filterFlag) {
		Map<String, Object> condition = new HashMap<>();
		condition.put("regionCode", regionCode);
		condition.put("regionName", regionName);
		List<TreeNodeVO<AreaCodeVO>> list = areaCodeService.queryAreaCodeTreeList(condition);

		List<TreeNodeVO<AreaCodeVO>> treeList = new ArrayList<TreeNodeVO<AreaCodeVO>>();
		if ("1".equals(showALL)) {
			TreeNodeVO<AreaCodeVO> vo = new TreeNodeVO<AreaCodeVO>();
			vo.setText("全部");
			treeList.add(vo);
		}
		treeList.addAll(list);

		return this.createResultRowsVO(treeList, true, "", list.size());
	}
	
	@ApiOperation(value = "查询区域信息列表", notes = "查询区域信息列表", httpMethod = "POST")
	@RequestMapping(value = "/info/list", method = RequestMethod.POST)
	@ResponseBody
	public ResultRowsVO<AreaCodeVO> queryAreaCodeList(String regionCode, String regionName, String filterFlag) {
		Map<String, Object> condition = new HashMap<>();
		condition.put("regionCode", regionCode);
		condition.put("regionName", regionName);
		List<AreaCodeVO> list = areaCodeService.queryAreaCodeList(condition);
		return this.createResultRowsVO(list, true, "", list.size());
	}

	/**
	 * 
	 * queryAreaCode:(查詢區域信息，這裡傳入區域ID，查找区域ID下面的子区域).
	 *
	 * @param regionCode
	 * @param showALL
	 * @param filterFlag
	 *            :有值-对用户的站点权限过滤；不传-进行过滤（2019-7）
	 * @return
	 */
	@ApiOperation(value = "查询区域信息下拉框", notes = "查询区域信息下拉框", httpMethod = "POST")
	@RequestMapping(value = "/childlist", method = RequestMethod.POST)
	@ResponseBody
	public ResultRowsVO<TreeNodeVO<AreaCodeVO>> queryAreaCodeTreeChild(String regionCode, String showALL,
			String filterFlag) {

		List<TreeNodeVO<AreaCodeVO>> list = areaCodeService.queryAreaCodeTreeChildList(regionCode, filterFlag);

		List<TreeNodeVO<AreaCodeVO>> treeList = new ArrayList<>();
		if ("1".equals(showALL)) {
			TreeNodeVO<AreaCodeVO> vo = new TreeNodeVO<AreaCodeVO>();
			vo.setText("全部");
			treeList.add(vo);
		}
		treeList.addAll(list);

		return this.createResultRowsVO(treeList, true, "", list.size());
	}

	/**
	 * 
	 * insertAreaCode:(插入区域信息).
	 *
	 * @param areaCodeVO
	 * @return
	 */
	@ApiOperation(value = "新增区域信息", notes = "新增区域信息", httpMethod = "POST")
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	public ResultDataVO<String> insertAreaCode(String areaCodeVO) {
		boolean flag = false;
		if (HttpServiceUtil.isAccessUrl(getSessionId(), request)) {
			AreaCodeVO AreaCodeVO = JSON.parseObject(areaCodeVO, AreaCodeVO.class);
			flag = areaCodeService.insertAreaCode(AreaCodeVO);
			String level = "";
			if (AreaCodeVO.getLevel() == 1) {
				level = "省级";
			} else if (AreaCodeVO.getLevel() == 2) {
				level = "城市";
			} else if (AreaCodeVO.getLevel() == 3) {
				level = "区县";
			}

		}
		return this.createResultDataVO(null, flag, null);
	}

	/**
	 * 
	 * updateAreaCode:(更新区域信息).
	 *
	 * @return
	 */
	@ApiOperation(value = "更新区域信息", notes = "更新区域信息", httpMethod = "POST")
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	@ResponseBody
	public ResultDataVO<String> updateAreaCode(String areaCodeVO) {
		boolean flag = false;
		if (HttpServiceUtil.isAccessUrl(getSessionId(), request)) {
			AreaCodeVO AreaCodeVO = JSON.parseObject(areaCodeVO, AreaCodeVO.class);
			flag = areaCodeService.updateAreaCode(AreaCodeVO);
			String level = "";
			if (AreaCodeVO.getLevel() == 1) {
				level = "省级";
			} else if (AreaCodeVO.getLevel() == 2) {
				level = "城市";
			} else if (AreaCodeVO.getLevel() == 3) {
				level = "区县";
			}

		}
		return this.createResultDataVO(null, flag, null);
	}

	/**
	 * 
	 * deleteAreaCode:(删除区域信息).
	 *
	 * @param regionCodes
	 * @return
	 */
	@ApiOperation(value = "删除区域信息", notes = "删除区域信息", httpMethod = "POST")
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	@ResponseBody
	public ResultDataVO<String> deleteAreaCode(String regionCodes) {
		boolean flag = false;
		if (HttpServiceUtil.isAccessUrl(getSessionId(), request)) {
			List<String> list = StringUtil.splitToList(regionCodes);
			flag = areaCodeService.deleteAreaCode(list);
		}
		return this.createResultDataVO(null, flag, null);
	}

	/**
	 * 
	 * queryAreaInfor:根据父级编码查询省市区信息
	 * 
	 * @param regionCode
	 * @return ResultRowsVO<AreaCodeVO>
	 */
	@ApiOperation(value = "根据父级编码查询省市区信息", notes = "根据父级编码查询省市区信息", httpMethod = "POST")
	@RequestMapping(value = "/tree", method = RequestMethod.POST)
	@ResponseBody
	public ResultRowsVO<AreaCodeVO> queryAreaInfor(String regionCode, Integer level) {
		ResultRowsVO<AreaCodeVO> result = new ResultRowsVO<AreaCodeVO>();
		List<AreaCodeVO> list = areaCodeService.queryAreaInfor(regionCode, level);

		Collections.sort(list, new Comparator<AreaCodeVO>() {

			@Override
			public int compare(AreaCodeVO arg1, AreaCodeVO arg2) {
				Collator instance = Collator.getInstance(Locale.CHINA);
				return instance.compare(arg1.getRegionName(), arg2.getRegionName());

			}
		});
		result.setRows(list);
		result.setTotal(list.size());
		return result;
	}

	@ApiOperation(value = "获取区域信息列表", notes = "获取区域信息列表", httpMethod = "POST")
	@RequestMapping(value = "/config", method = RequestMethod.POST)
	@ResponseBody
	public ResultRowsVO<AreaCodeVO> queryAreaCode(String regionCode, String regionName, String userRight) {
		Map<String, Object> condition = new HashMap<>();
		condition.put("regionCode", regionCode);
		condition.put("regionName", regionName);

		List<AreaCodeVO> list = areaCodeService.queryAreaCodeList(condition);
		return this.createResultRowsVO(list, true, "", list.size());
	}
}
